<?php

/**
 * This is the model class for table "es_orders".
 *
 * The followings are the available columns in table 'es_orders':
 * @property string $order_id
 * @property string $order_sn
 * @property integer $user_id
 * @property string $total
 * @property integer $order_status
 * @property integer $shipping_status
 * @property integer $pay_status
 * @property integer $confirm_status
 * @property integer $country
 * @property integer $province
 * @property integer $city
 * @property integer $district
 * @property string $address
 * @property string $zipcode
 * @property string $name
 * @property string $telephone
 * @property integer $order_time
 * @property integer $pay_time
 * @property integer $shipping_time
 * @property integer $confirm_time
 */
class Orders extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Orders the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'es_orders';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('order_sn, user_id, total, address, zipcode, name, telephone', 'required'),
			array('user_id, order_status, shipping_status, pay_status, confirm_status, country, province, city, district, order_time, pay_time, shipping_time, confirm_time', 'numerical', 'integerOnly'=>true),
			array('order_id, total', 'length', 'max'=>10),
			array('order_sn', 'length', 'max'=>20),
			array('address', 'length', 'max'=>255),
			array('zipcode', 'length', 'max'=>6),
			array('name', 'length', 'max'=>45),
			array('telephone', 'length', 'max'=>11),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('order_id, order_sn, user_id, total, order_status, shipping_status, pay_status, confirm_status, country, province, city, district, address, zipcode, name, telephone, order_time, pay_time, shipping_time, confirm_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'orderLists'=>array(self::HAS_MANY,'OrderList','order_id'),
			'user'=>array(self::BELONGS_TO,'User','user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'order_id' => 'Order',
			'order_sn' => 'Order Sn',
			'user_id' => 'User',
			'total' => 'Total',
			'order_status' => 'Order Status',
			'shipping_status' => 'Shipping Status',
			'pay_status' => 'Pay Status',
			'confirm_status' => 'Confirm Status',
			'country' => 'Country',
			'province' => 'Province',
			'city' => 'City',
			'district' => 'District',
			'address' => 'Address',
			'zipcode' => 'Zipcode',
			'name' => 'Name',
			'telephone' => 'Telephone',
			'order_time' => 'Order Time',
			'pay_time' => 'Pay Time',
			'shipping_time' => 'Shipping Time',
			'confirm_time' => 'Confirm Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('order_id',$this->order_id,true);
		$criteria->compare('order_sn',$this->order_sn,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('total',$this->total,true);
		$criteria->compare('order_status',$this->order_status);
		$criteria->compare('shipping_status',$this->shipping_status);
		$criteria->compare('pay_status',$this->pay_status);
		$criteria->compare('confirm_status',$this->confirm_status);
		$criteria->compare('country',$this->country);
		$criteria->compare('province',$this->province);
		$criteria->compare('city',$this->city);
		$criteria->compare('district',$this->district);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('zipcode',$this->zipcode,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->compare('order_time',$this->order_time);
		$criteria->compare('pay_time',$this->pay_time);
		$criteria->compare('shipping_time',$this->shipping_time);
		$criteria->compare('confirm_time',$this->confirm_time);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 
	 * @return string the string stand for the order status
	 */
	public function orderStatusToText(){
		if($this->order_status == 1){
			return 'payed';
		}
		if($this->order_status == 2){
			return 'sended';
		}
		if($this->order_status == 3){
			return 'completed';
		}
		return 'unkownStatus';
	}
	
	/**
	 * @return string the string of the shipping status
	 */
	public function shippingStatusToText()
	{
		switch($this->shipping_status)
		{
			case 0:
				return 'unshipping';
			case 1:
				return 'shippinged';
			case 2:
				return 'completed';
		}
	}
	
	/**
	 * @return string the string of the province name;
	 */
	public function getProvinceText(){
		$region = Region::model()->findByPk($this->province);
		return $region->region_name;
	}
	
	/**
	 * @return string the string of the city name;
	 */
	public function getCityText(){
		$region = Region::model()->findByPk($this->city);
		return $region->region_name;
	}
	
	/**
	 * @return string the string of the district name;
	 */
	public function getDistrictText(){
		$region = Region::model()->findByPk($this->district);
		return $region->region_name;
	}
}